/**
 * Filename: PublishWallAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/19 9:44
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.PublishActivity;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.Logger;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;

import java.util.List;

public class PublishWallAdapter extends RecyclerView.Adapter<PublishWallAdapter.ViewHolder> {
    public static final int TYPE_FOOTER = 0;
    public static final int TYPE_NORMAL = 1;
    private View mFooterView;
    private Context mContext;
    private List<String> mDatas;
    private ImageLoader imageLoader;
    private Resources mResource;

    public PublishWallAdapter(Context mContext, List<String> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        imageLoader = ImageLoader.getInstance();
    }

    public void setResource(Resources mResource){
        this.mResource = mResource;
        if (mFooterView!=null && mResource!=null){
            ((ImageView)mFooterView.findViewById(R.id.iv_add_pic_more)).setImageDrawable(mResource.getDrawable(mResource.getIdentifier("ic_add_photo", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
        }
    }

    public  class ViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView iv_group_grid_image;
        ImageView        iv_group_grid_delete_image;

        public ViewHolder(final View itemView) {
            super(itemView);
            iv_group_grid_image	= (SimpleDraweeView) itemView
                    .findViewById(R.id.iv_group_grid_image);
            iv_group_grid_delete_image = (ImageView) itemView
                    .findViewById(R.id.iv_group_grid_delete_image);
            itemView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    ViewGroup.LayoutParams vlp = itemView.getLayoutParams();
                    vlp.height = itemView.getMeasuredWidth();
                    vlp.width = itemView.getMeasuredWidth();
                    itemView.setLayoutParams(vlp);
                    return true;
                }
            });
        }

        public void bindData(final String item, final int position)
        {
            if (item != null)
            {
                if (item.startsWith("http")){
                    Uri parse = Uri.parse(item);
                    ImageOptHelper.showFrescoNative(parse, iv_group_grid_image, mContext);
                }else{
                    Uri uri = Uri.parse("file://"+item);
                    ImageOptHelper.showFrescoNative(uri, iv_group_grid_image, mContext);
                }
                // 删除
                iv_group_grid_delete_image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDatas.remove(position);
                        SelectedImg.mSelectedImage.remove(position);
                        notifyDataSetChanged();
                        mContext.sendBroadcast(new Intent(ACTIONs.actionUpdatePicBigSmall));
                    }
                });
            }
        }
    }

    private String getItem(int position) {
        return mDatas.get(position);
    }

    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount()-1);
    }
    public View getFooterView() {
        return mFooterView;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == getItemCount()-1) return TYPE_FOOTER;
        return TYPE_NORMAL;
    }

    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mFooterView != null && viewType == TYPE_FOOTER) return new ViewHolder(mFooterView);
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_grid_image, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(mFooterView != null && position == getItemCount()-1){
            return;
        }
        String listItems = getItem(position);
//        Uri item = Uri.parse(listItems);
        holder.bindData(listItems , position);
        if (mResource!=null){
            holder.iv_group_grid_delete_image.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("pictures_cancel", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
        }
        holder.itemView.setTag(listItems);
    }

    @Override
    public int getItemCount() {
        return mFooterView == null ? mDatas.size() :mDatas.size()+1;
    }
}
